home *** CD-ROM | disk | FTP | other *** search
/ Merciful 5 / Merciful - Disc 5.iso / software / r / rtg_master / rtgmasterv21.0dev.lha / demos / flame / flame.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-14  |  7.3 KB  |  307 lines

  1. /*
  2.         Flame
  3.  
  4.         A demo of rtg.library
  5.  
  6.         Written by Thomas & Hans-Jörg Frieden
  7.                    Schlossstr. 176
  8.                    54293 Trier
  9.                    tfrieden@fax.uni-trier.de
  10.                    hfrieden@fix.uni-trier.de
  11.  
  12. */
  13. //* "Includes"
  14. #include <stdlib.h>
  15. #include <proto/utility.h>
  16. #include <proto/exec.h>
  17. #include <clib/rtgmaster_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/utility_protos.h>
  20. #include <pragmas/rtgmaster_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/utility_pragmas.h>
  23. #include <exec/memory.h>
  24. #include <utility/tagitem.h>
  25. #include <rtgmaster/rtgmaster.h>
  26. #include <rtgmaster/rtgsublibs.h>
  27. #include <rtgmaster/rtgc2p.h>
  28. #include <proto/rtgmaster.h>
  29. #include "timer.h"
  30. #include <string.h>
  31. #include <stdio.h>
  32. //*
  33. //* "Defines"
  34. #define XSIZE 80
  35. #define YSIZE 65
  36. #define YSCRN 60
  37. #define MSIZE (XSIZE*YSIZE)
  38. #define DECAY 3
  39. #define CBUF    76800
  40. //*
  41. //* "Pragmas"
  42. extern void __asm CopyFrame(register __a0 UBYTE *adr, 
  43.                             register __d0 ULONG size);
  44. extern void __asm CopyFrame2(register __a0 UBYTE *adr, 
  45.                              register __d0 ULONG aize);
  46. extern void __asm DrawMeter(register __a0 APTR buf, 
  47.                             register __d0 ULONG fps);
  48.  
  49. UBYTE MouseButton(void);
  50. //*
  51. //* "Globals"
  52. struct RtgScreen *RtgScreen;
  53. struct ScreenReq *sr;
  54. struct RTGMasterBase *RTGMasterBase;
  55. struct Library *UtilityBase;
  56. struct TagItem rtag[] = {
  57.     smr_MinWidth,       320,
  58.     smr_MinHeight,      200,
  59.     smr_MaxWidth,       640,
  60.     smr_MaxHeight,      512,
  61.     smr_ChunkySupport,  512,
  62.     smr_PlanarSupport,  -1,
  63.     TAG_DONE,           NULL
  64. };
  65.  
  66. struct TagItem gtag[] = {
  67.     grd_BytesPerRow,    0,
  68.     grd_Width,          0,
  69.     grd_Height,         0,
  70.     grd_Depth,          0,
  71.     grd_PixelLayout,    0,
  72.     grd_ColorSpace,     0,
  73.     grd_PlaneSize,      0,
  74.     TAG_DONE,           0
  75. };
  76.  
  77. struct TagItem tacks[] = {
  78.     TAG_DONE,0
  79. };
  80.  
  81. UBYTE *sc1;
  82. UBYTE scrn[MSIZE];
  83. ULONG cmap[800];
  84. UBYTE blah;
  85. BOOL Planar;
  86. UBYTE *cbuf=NULL;
  87. ULONG width;
  88. UBYTE *sadr;
  89. ULONG num,avg;
  90. ULONG c2psignal;
  91. //*
  92. //* "NewFrame"
  93. void NewFrame(void) {
  94.     int i,x;
  95.     UBYTE *a;
  96.  
  97.     extern void GenFrame(void);
  98.  
  99.     a=&scrn[MSIZE-1]; i=XSIZE;
  100.  
  101.     while (i>0) {
  102.         x=rand();
  103.         if (x>RAND_MAX/2) *a=255;
  104.         else *a=0;
  105.         i--; a--;
  106.     }
  107.  
  108.     i=MSIZE-XSIZE-1;
  109.     a=scrn;
  110.  
  111.     GenFrame();
  112. }
  113. //*
  114. //* "DrawFrame"
  115. void DrawFrame(struct RtgScreen *s, ULONG size, ULONG ticks) {
  116.     if (blah==0) CopyFrame(sadr, size);
  117.     else CopyFrame2(sadr,size);
  118.     DrawMeter(sadr, ticks);
  119. }
  120. //*
  121. //* "DrawFrameP"
  122. void DrawFrameP(struct RtgScreen *s, ULONG size, ULONG ticks) {
  123.     UBYTE *adr;
  124.  
  125.     adr=cbuf;                               // Draw into chunky buffer first
  126.     if (blah==0) CopyFrame(adr, 320);
  127.     else CopyFrame2(adr,320);
  128.     DrawMeter(adr, ticks);
  129.     adr = sadr;
  130.     CallRtgC2P(s,adr,cbuf,c2psignal,0,0,320,200,c2p_Selected);
  131.     Wait(1<<c2psignal);
  132. }
  133. //*
  134. //* "fail"
  135. void fail(void) {
  136.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  137.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  138.     if (UtilityBase) CloseLibrary(UtilityBase);
  139.     if (cbuf) FreeMem(cbuf, CBUF);
  140.     exit(0L);
  141. }
  142. //*
  143. //* "main"
  144. void main(int argc, char **argv) {
  145.    /*
  146.     * Since this is a demo, I don't check anything at all
  147.     * and simply assume that every open went ok... 8-)
  148.     */
  149.     int i, x;
  150.     struct TagItem *tag;
  151.     UBYTE rr, rg, rb;
  152.     ULONG size;
  153.     ULONG ticks=0, max=0, min=300;
  154.  
  155.     if (argc>1 && stricmp(argv[1],"small")==0) blah=1; else blah=0;
  156.  
  157.     if (InitTimer()==FALSE) {
  158.         printf("Unable to open timer device\n");
  159.         fail();
  160.     }
  161.  
  162.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  163.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  164.     c2psignal=AllocSignal(-1);
  165.     sr = RtgScreenModeReq(rtag);
  166.  
  167.     if (sr==NULL) fail();
  168.     if (c2psignal==-1) fail();
  169.     RtgScreen = OpenRtgScreen(sr, tacks);
  170.  
  171.     GetRtgScreenData(RtgScreen, gtag);
  172.  
  173.     tag=FindTagItem(grd_BytesPerRow, gtag);
  174.     size = tag->ti_Data;
  175.  
  176.     tag=FindTagItem(grd_Width, gtag);
  177.     width = tag->ti_Data;
  178.  
  179.     tag=FindTagItem(grd_PixelLayout, gtag);
  180.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  181.         printf("Screenmode not supported\n");
  182.         fail();
  183.     }
  184.  
  185.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  186.     else Planar = FALSE;
  187.  
  188.     printf("Screen is %ld x %ld x %ld\n", gtag[1].ti_Data, gtag[2].ti_Data, gtag[3].ti_Data);
  189.     printf("It has %ld bytes per row", size);
  190.     if (size>gtag[1].ti_Data) {
  191.         printf(", which means that the screen is wider than what you see\n");
  192.     } else printf("\n");
  193.  
  194.     if (Planar == TRUE) {
  195.         cbuf = AllocMem(CBUF, MEMF_CLEAR|MEMF_FAST);
  196.         if (cbuf==NULL) {
  197.             cbuf=AllocMem(CBUF, MEMF_CLEAR);
  198.             if (cbuf==NULL) {
  199.                 printf("Out of memory *SIGH*\n");
  200.                 fail();
  201.             }
  202.         }
  203.     }
  204.  
  205. if (gtag[3].ti_Data==8)
  206. {
  207.     cmap[0] = 256 * 65536;
  208.     rr = 0;
  209.     rg = 0;
  210.     rb = 0;
  211.     x = 1;
  212.     for (i = 0; i < 64; i++) {
  213.         cmap[x++] = rr * 0x1111111;
  214.         cmap[x++] = rg * 0x1111111;
  215.         cmap[x++] = rb * 0x1111111;
  216.         rr += 3;
  217.     }
  218.     for (i = 0; i < 127; i++) {
  219.         cmap[x++] = rr * 0x1111111;
  220.         cmap[x++] = rg * 0x1111111;
  221.         cmap[x++] = rb * 0x1111111;
  222.         rg += 3;
  223.     }
  224.     for (i = 0; i < 60; i++) {
  225.         cmap[x++] = rr * 0x1111111;
  226.         cmap[x++] = rg * 0x1111111;
  227.         cmap[x++] = rb * 0x1111111;
  228.         rb += 3;
  229.     }
  230.     for (i = 0; i < 4; i++) {
  231.         cmap[x++]=0xFFFFFFFF;
  232.         cmap[x++]=0xFFFFFFFF;
  233.         cmap[x++]=0xFFFFFFFF;
  234.     }
  235.     cmap[x++]=0;
  236. }
  237. else
  238. {
  239.    cmap[0] = 64 * 65536;
  240.     rr = 0;
  241.     rg = 0;
  242.     rb = 0;
  243.     x = 1;
  244.     for (i = 0; i < 16; i++) {
  245.         cmap[x++] = rr * 0x1111111;
  246.         cmap[x++] = rg * 0x1111111;
  247.         cmap[x++] = rb * 0x1111111;
  248.         rr += 12;
  249.     }
  250.     for (i = 0; i < 31; i++) {
  251.         cmap[x++] = rr * 0x1111111;
  252.         cmap[x++] = rg * 0x1111111;
  253.         cmap[x++] = rb * 0x1111111;
  254.         rg += 12;
  255.     }
  256.     for (i = 0; i < 12; i++) {
  257.         cmap[x++] = rr * 0x1111111;
  258.         cmap[x++] = rg * 0x1111111;
  259.         cmap[x++] = rb * 0x1111111;
  260.         rb += 12;
  261.     }
  262.     for (i = 0; i < 4; i++) {
  263.         cmap[x++]=0xFFFFFFFF;
  264.         cmap[x++]=0xFFFFFFFF;
  265.         cmap[x++]=0xFFFFFFFF;
  266.     }
  267.     cmap[x++]=0;
  268. }
  269.     LockRtgScreen(RtgScreen);
  270.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  271.     UnlockRtgScreen(RtgScreen);
  272.  
  273.     if (RtgScreen) {
  274.         LockRtgScreen(RtgScreen);
  275.         sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  276.  
  277.         Forbid();
  278.         num=0;
  279.         while(MouseButton()==0) {
  280.             StartClock();
  281.             NewFrame();
  282.             if (Planar==FALSE) DrawFrame(RtgScreen, size, ticks);
  283.             else DrawFrameP(RtgScreen, size, ticks);
  284.             ticks=StopClock();
  285.             num++;
  286.             avg+=ticks;
  287.             if (max<ticks) max=ticks;
  288.             if (min>ticks) min=ticks;
  289.         }
  290.         Permit();
  291.         UnlockRtgScreen(RtgScreen);
  292.         CloseRtgScreen(RtgScreen);
  293.     }
  294.     if (max==0) max=1;
  295.     if (min==0) min=1;
  296.     printf("Min Frame rate: %ld\n", max);
  297.     printf("Max Frame rate: %ld\n", min);
  298.     printf("Avg Frame rate: %ld\n", avg/num);
  299.     CloseTimer();
  300.     FreeSignal(c2psignal);
  301.     FreeMem(cbuf,CBUF);
  302.     CloseLibrary((struct Library *)RTGMasterBase);
  303.     CloseLibrary(UtilityBase);
  304. }
  305. //*
  306.  
  307.